﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;

namespace SiteFox.Business.Manager
{
    public static class User
    {
        public static Model.User GetUser(string _SessionGuid) 
        {
            Model.User _User = null;
            var _Entities = new Model.Entities(Data.WebConfig.GetConnectionString());
            var _Query = from c in _Entities.UserSession where c.SessionGuid == _SessionGuid select c;
            var __UserSession = _Query.Single();
            if (__UserSession != null)
            {
                _User = __UserSession.User;
            }
            return _User;
        }
        public static List<Model.User> GetAllUsers() 
        {
            var _Entities = new Model.Entities(Data.WebConfig.GetConnectionString());
            var _Query = from c in _Entities.User select c;
            return _Query.ToList();
        }
        public static string GetSHA512(string _Text)
        {
            UnicodeEncoding UE = new UnicodeEncoding();
            byte[] _HashValue;
            byte[] _Message = UE.GetBytes(_Text);
            SHA512Managed hashString = new SHA512Managed();
            string hex = "";
            _HashValue = hashString.ComputeHash(_Message);
            foreach (byte x in _HashValue)
            {
                hex += String.Format("{0:x2}", x);
            }
            return hex;
        }
        public static void Create(Model.User _User)
        {
            var _Entities = new Model.Entities(Data.WebConfig.GetConnectionString());
            _Entities.AddToUser(_User);
            _Entities.SaveChanges();
        }
        public static void Edit(Model.User _User)
        {
            var _Entities = new Model.Entities(Data.WebConfig.GetConnectionString());
            var _Query = from c in _Entities.User where c.ID == _User.ID select c;
            var __User = _Query.Single();
            __User.Password = _User.Password;
            _Entities.SaveChanges();
        }
        public static void Delete(int _ID)
        {
            var _Entities = new Model.Entities(Data.WebConfig.GetConnectionString());
            var _Query = from c in _Entities.User where c.ID == _ID select c;
            var _User = _Query.Single();
            _Entities.User.DeleteObject(_User);
            _Entities.SaveChanges();
        }
        public static Model.User Validate(string _Username, string _Password) 
        {
            var _HeshPass = GetSHA512(_Password);
            var _Entities = new Model.Entities(Data.WebConfig.GetConnectionString());
            var _Query = from c in _Entities.User where c.Username == _Username select c;
            var _User = _Query.SingleOrDefault();
            if (_User != null) 
            {
                if (_User.Password == _HeshPass) 
                {
                    return _User;
                }
            }
            return null;
        }
        public static void AddSession(Model.UserSession _Session) 
        {
            var _Entities = new Model.Entities(Data.WebConfig.GetConnectionString());
            _Entities.AddToUserSession(_Session);
            _Entities.SaveChanges();
        }
        public static bool UpdateSession(string _SessionGuid) 
        {
            bool _IsUpdated = false;
            var _Entities = new Model.Entities(Data.WebConfig.GetConnectionString());
            var _Query = from c in _Entities.UserSession where c.SessionGuid == _SessionGuid select c;
            var __UserSession = _Query.Single();
            if (__UserSession != null) 
            { 
                __UserSession.LastSessionActiveDate = DateTime.Now;
                _Entities.SaveChanges();
                _IsUpdated = true;
            }
            return _IsUpdated;
        }
    }
}
